package com.example.industry.controller;

import com.example.industry.entity.User;
import com.example.industry.enums.Roles;
import com.example.industry.service.ProjectService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/project")
public class ProjectController
{
    @Resource
    private ProjectService projectService;

    /**
     * 创建项目
     * @param projectName 项目名称
     * @param projectIntroduction 项目介绍
     * @return 是否成功
     */
    @PostMapping(value = "/create")
    public boolean createProject(@RequestParam(value = "projectName") String projectName,
                                 @RequestParam(value = "projectIntroduction") String projectIntroduction,
                                 HttpSession session)
    {
        User user = (User)session.getAttribute("user");
        if (user == null) return false;
        return projectService.createProject(projectName, user.getUserId(), projectIntroduction);
    }

    /**
     * 删除项目
     * @param projectId 项目Id
     * @return 是否成功
     */
    @DeleteMapping(value = "/{projectId}")
    public boolean deleteProject(@PathVariable int projectId)
    {
        return projectService.deleteProjectById(projectId);
    }

    /**
     * 更新项目
     * @param projectId 项目Id
     * @param projectName 项目名
     * @param projectIntroduction 项目介绍
     * @return 是否成功
     */
    @PutMapping(value = "/update")
    public boolean updateProject(@RequestParam(value = "projectId") Integer projectId,
                                 @RequestParam(value = "projectName") String projectName,
                                 @RequestParam(value = "projectIntroduction") String projectIntroduction)
    {
        return projectService.updateProject(projectId, projectName, projectIntroduction);
    }

    /**
     * 获取一个项目中的所有成员
     * @param projectId 项目Id
     * @return 用户集合
     */
    @GetMapping(value = "/users/all")
    public List<User> findUsersByProjectIdAndRole(@RequestParam(value = "projectId") Integer projectId)
    {
        return projectService.findUsersByProjectIdAndRole(projectId, Roles.ALL);
    }

    /**
     * 获取一个项目中的拥有者成员
     * @param projectId 项目Id
     * @return 用户集合
     */
    @GetMapping(value = "/users/owner")
    public List<User> findOwnerUsersByProjectId(@RequestParam(value = "projectId") Integer projectId)
    {
        return projectService.findUsersByProjectIdAndRole(projectId, Roles.OWNER);
    }

    /**
     * 获取一个项目中的管理员成员
     * @param projectId 项目Id
     * @return 用户集合
     */
    @GetMapping(value = "/users/admin")
    public List<User> findAdminUsersByProjectId(@RequestParam(value = "projectId") Integer projectId)
    {
        return projectService.findUsersByProjectIdAndRole(projectId, Roles.ADMIN);
    }

    /**
     * 获取一个项目中的普通成员
     * @param projectId 项目Id
     * @return 用户集合
     */
    @GetMapping(value = "/users/common")
    public List<User> findCommonUsersByProjectId(@RequestParam(value = "projectId") Integer projectId)
    {
        return projectService.findUsersByProjectIdAndRole(projectId, Roles.COMMON);
    }

    /**
     * 修改一个项目中的成员的角色
     * @param projectId 项目Id
     * @param userId 用户Id
     * @param role 角色
     * @return 是否成功
     */
    @PostMapping(value = "/change_role")
    public boolean updateUserRoleInProject(@RequestParam(value = "projectId") Integer projectId,
                                           @RequestParam(value = "userId") Integer userId,
                                           @RequestParam(value = "role") Roles role)
    {
        return projectService.updateUserRoleInProject(projectId, userId, role);
    }

    /**
     * 删除项目中的成员
     * @param projectId 项目Id
     * @param userId 用户Id
     * @return 是否成功
     */
    @DeleteMapping(value = "/delete_user")
    public boolean deleteUserInProjectByUserId(@RequestParam(value = "projectId") Integer projectId,
                                               @RequestParam(value = "userId") Integer userId)
    {
        return projectService.deleteUserInProjectByUserId(projectId, userId);
    }
}
